<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="询价单编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入询价单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目ID" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="希望报价日" prop="expectedQuotationDate">
        <el-date-picker clearable
          v-model="queryParams.expectedQuotationDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择希望报价日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="希望到货日" prop="expectedDeliveryDate">
        <el-date-picker clearable
          v-model="queryParams.expectedDeliveryDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择希望到货日">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['scm:ScmInquiriesIn:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scm:ScmInquiriesIn:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scm:ScmInquiriesIn:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['scm:ScmInquiriesIn:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ScmInquiriesInList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="询价单编号" align="center" prop="code" />
      <el-table-column label="项目ID" align="center" prop="projectId" />
      <el-table-column label="产品ID" align="center" prop="productId" />
      <el-table-column label="材质" align="center" prop="material" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="单台数量" align="center" prop="onceQuantity" />
      <el-table-column label="单台单位" align="center" prop="onceUnit" />
      <el-table-column label="月数量" align="center" prop="monthlyQuantity" />
      <el-table-column label="月单位" align="center" prop="monthlyUnit" />
      <el-table-column label="年数量" align="center" prop="yearlyQuantity" />
      <el-table-column label="年单位" align="center" prop="yearlyUnit" />
      <el-table-column label="一次性数量" align="center" prop="disposableQuantity" />
      <el-table-column label="一次性单位" align="center" prop="disposableUnit" />
      <el-table-column label="一次性试做数量" align="center" prop="disposableTryQuantity" />
      <el-table-column label="一次性试做单位" align="center" prop="disposableTryUnit" />
      <el-table-column label="试做数量" align="center" prop="tryQuantity" />
      <el-table-column label="试做单位" align="center" prop="tryUnit" />
      <el-table-column label="量产数量" align="center" prop="massQuantity" />
      <el-table-column label="量产单位" align="center" prop="massUnit" />
      <el-table-column label="希望报价日" align="center" prop="expectedQuotationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expectedQuotationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="希望到货日" align="center" prop="expectedDeliveryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expectedDeliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求资料" align="center" prop="expectedDatum" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:ScmInquiriesIn:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:ScmInquiriesIn:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户询价单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="ScmInquiriesInRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="询价单编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入询价单编号" />
        </el-form-item>
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID" />
        </el-form-item>
        <el-form-item label="材质" prop="material">
          <el-input v-model="form.material" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-input v-model="form.color" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="单台数量" prop="onceQuantity">
          <el-input v-model="form.onceQuantity" placeholder="请输入单台数量" />
        </el-form-item>
        <el-form-item label="单台单位" prop="onceUnit">
          <el-input v-model="form.onceUnit" placeholder="请输入单台单位" />
        </el-form-item>
        <el-form-item label="月数量" prop="monthlyQuantity">
          <el-input v-model="form.monthlyQuantity" placeholder="请输入月数量" />
        </el-form-item>
        <el-form-item label="月单位" prop="monthlyUnit">
          <el-input v-model="form.monthlyUnit" placeholder="请输入月单位" />
        </el-form-item>
        <el-form-item label="年数量" prop="yearlyQuantity">
          <el-input v-model="form.yearlyQuantity" placeholder="请输入年数量" />
        </el-form-item>
        <el-form-item label="年单位" prop="yearlyUnit">
          <el-input v-model="form.yearlyUnit" placeholder="请输入年单位" />
        </el-form-item>
        <el-form-item label="一次性数量" prop="disposableQuantity">
          <el-input v-model="form.disposableQuantity" placeholder="请输入一次性数量" />
        </el-form-item>
        <el-form-item label="一次性单位" prop="disposableUnit">
          <el-input v-model="form.disposableUnit" placeholder="请输入一次性单位" />
        </el-form-item>
        <el-form-item label="一次性试做数量" prop="disposableTryQuantity">
          <el-input v-model="form.disposableTryQuantity" placeholder="请输入一次性试做数量" />
        </el-form-item>
        <el-form-item label="一次性试做单位" prop="disposableTryUnit">
          <el-input v-model="form.disposableTryUnit" placeholder="请输入一次性试做单位" />
        </el-form-item>
        <el-form-item label="试做数量" prop="tryQuantity">
          <el-input v-model="form.tryQuantity" placeholder="请输入试做数量" />
        </el-form-item>
        <el-form-item label="试做单位" prop="tryUnit">
          <el-input v-model="form.tryUnit" placeholder="请输入试做单位" />
        </el-form-item>
        <el-form-item label="量产数量" prop="massQuantity">
          <el-input v-model="form.massQuantity" placeholder="请输入量产数量" />
        </el-form-item>
        <el-form-item label="量产单位" prop="massUnit">
          <el-input v-model="form.massUnit" placeholder="请输入量产单位" />
        </el-form-item>
        <el-form-item label="希望报价日" prop="expectedQuotationDate">
          <el-date-picker clearable
            v-model="form.expectedQuotationDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择希望报价日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="希望到货日" prop="expectedDeliveryDate">
          <el-date-picker clearable
            v-model="form.expectedDeliveryDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择希望到货日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="需求资料" prop="expectedDatum">
          <el-input v-model="form.expectedDatum" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ScmInquiriesIn">
import { listScmInquiriesIn, getScmInquiriesIn, delScmInquiriesIn, addScmInquiriesIn, updateScmInquiriesIn } from "@/api/scm/ScmInquiriesIn";

const { proxy } = getCurrentInstance();

const ScmInquiriesInList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    projectId: null,
    productId: null,
    expectedQuotationDate: null,
    expectedDeliveryDate: null,
    expectedDatum: null,
  },
  rules: {
    projectId: [
      { required: true, message: "项目ID不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "产品ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询客户询价单列表 */
function getList() {
  loading.value = true;
  listScmInquiriesIn(queryParams.value).then(response => {
    ScmInquiriesInList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    code: null,
    projectId: null,
    productId: null,
    material: null,
    specification: null,
    color: null,
    onceQuantity: null,
    onceUnit: null,
    monthlyQuantity: null,
    monthlyUnit: null,
    yearlyQuantity: null,
    yearlyUnit: null,
    disposableQuantity: null,
    disposableUnit: null,
    disposableTryQuantity: null,
    disposableTryUnit: null,
    tryQuantity: null,
    tryUnit: null,
    massQuantity: null,
    massUnit: null,
    expectedQuotationDate: null,
    expectedDeliveryDate: null,
    expectedDatum: null,
    remarks: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("ScmInquiriesInRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加客户询价单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScmInquiriesIn(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改客户询价单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ScmInquiriesInRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateScmInquiriesIn(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmInquiriesIn(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除客户询价单编号为"' + _ids + '"的数据项？').then(function() {
    return delScmInquiriesIn(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/ScmInquiriesIn/export', {
    ...queryParams.value
  }, `ScmInquiriesIn_${new Date().getTime()}.xlsx`)
}

getList();
</script>
